//
//  SFLeiXingCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/7.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SFLeiXingCtrl.h"
#import "SelectNavView.h"
#import "SFTabHeadView.h"
#import "SFLeixingCell.h"
#import "PageModels.h"
#import "Fishing_chare_Mode.h"
#import "FishingChareDataMode.h"
#import "CGFdbmBase.h"

@interface SFLeiXingCtrl ()<UITableViewDataSource,UITableViewDelegate,SFLeixingCellClick,UITextFieldDelegate>
@property (nonatomic,strong) SelectNavView *naV;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,assign) CGFloat cellHight;
@property (nonatomic,strong) PageModels *pageModel;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) Fishing_chare_Mode *chareModel;
@end

@implementation SFLeiXingCtrl

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray= [NSMutableArray array];
    }
    return _dataArray;
}

-(void)DataFromSQL{
    [CGFdbmBase OpenSQlWhiPathName:@"FirstPage.db"];
    NSInteger TableCount = [CGFdbmBase getTableItemCount:@"FirstPage"];
    
    if (TableCount!=0) {
        [self.dataArray addObjectsFromArray:[CGFdbmBase ListWhith:@"FirstPage" dicName:@"Dic"]];
        self.pageModel = [[PageModels alloc]initWithDictionary:_dataArray.firstObject error:nil];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    [self CreatNavView];
}
-(void)CreatNavView{
    self.naV = [[SelectNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.naV.titile.text = @"选择收费方式";
    self.naV.share.titleLabel.font = font(SCREEN_WIDTH*0.05);
    [self.naV.share setTitle:@"确定" forState:UIControlStateNormal];
    self.naV.share.y = 21;
    self.naV.share.x = SCREEN_WIDTH - 60;
    [self.view addSubview:self.naV];
    [self.naV.bacBtn addTarget:self action:@selector(Back) forControlEvents:UIControlEventTouchUpInside];
    [self.naV.share addTarget:self action:@selector(Tijiao) forControlEvents:UIControlEventTouchUpInside];
}
-(void)Back{
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {//push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{//present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }

}
-(void)Tijiao{
    
    if (_SFBlock) {
        _SFBlock(self.selectResult,self.charge_type,self.charge_sum);
    }
    
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {//push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{//present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    [_tableview registerNib:[UINib nibWithNibName:@"SFLeixingCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    [self.view addSubview:_tableview];
    
    _tableview.tableFooterView = [UIView new];
    
    [self DataFromSQL];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.pageModel.fishing_chare_mode.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    SFTabHeadView *head  = [[NSBundle mainBundle]loadNibNamed:@"SFTabHeadView" owner:nil options:nil].lastObject;
    head.frame = CGRectMake(0, 0, SCREEN_WIDTH, 35);
    
    Fishing_chare_Mode *model = self.pageModel.fishing_chare_mode[section];
    head.title.text = model.name;
    
    head.Shoufei.text = model.tips;
    
    return head;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorStyle = 0;
    
    SFLeixingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    Fishing_chare_Mode *model = self.pageModel.fishing_chare_mode[indexPath.section];

    cell.charactAry = model.data;
    self.cellHight = cell.cellhight;
    cell.tag = 10000+indexPath.section;
    cell.cellTag = cell.tag;
    cell.deleget = self;
    return cell;
}
/**
 *  点击cell上的btn所调代理方法
 *
 *  @param cellTag cell的tag值
 *
 *  @param title   btn上的文字
 */
-(void)SFLeixingCellClick:(NSInteger)cellTag AndBtnTitle:(NSString *)title AndClickId:(NSString *)Id AndPrice:(NSString *)price{
    
    for (int i=0; i<self.pageModel.fishing_chare_mode.count; i++) {
        SFLeixingCell *cell = [self.view.superview viewWithTag:i+10000];
        
        if (cell.cellTag!=cellTag) {
            [cell AllBlack];
        }
    }
    
  
    if ([title isEqualToString:@"自定义"]) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请输入金额" message:nil preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            for (int i=0; i<self.pageModel.fishing_chare_mode.count; i++) {
                SFLeixingCell *cell = [self.view.superview viewWithTag:i+10000];
                
                [cell AllBlack];
            }
        }];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
 
            [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
            
            
            
            UITextField *text = alertController.textFields.firstObject;
           
            if (text.text.length!=0) {
                
                NSInteger count = cellTag-10000;
                
                 Fishing_chare_Mode *model = self.pageModel.fishing_chare_mode[cellTag-10000];
                if (count==1){
                    
                    self.selectResult = [NSString stringWithFormat:@"%@ %@元/斤",model.name,text.text];
                    
                }else if (count==2){
                    self.selectResult = [NSString stringWithFormat:@"%@ %@元/天",model.name,text.text];
                }else if (count==3){
                    self.selectResult = [NSString stringWithFormat:@"%@ %@元/小时",model.name,text.text];
                }
                
                
                if (_SFBlock) {
                    _SFBlock(self.selectResult,model.type,text.text);
                }
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self.navigationController popViewControllerAnimated:YES];

                });

                
            }else{
                for (int i=0; i<self.pageModel.fishing_chare_mode.count; i++) {
                    SFLeixingCell *cell = [self.view.superview viewWithTag:i+10000];
                    
                    [cell AllBlack];
                }
            }
        }];
        
        
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            textField.keyboardType = UIKeyboardTypeNumberPad;
        }];
        
        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        
        [self presentViewController:alertController animated:YES completion:nil];

    }else{
        
            NSInteger count = cellTag-10000;
        
        Fishing_chare_Mode *model = self.pageModel.fishing_chare_mode[cellTag-10000];
        self.charge_type = model.type;
        self.charge_sum = price;
        
        if (count==0) {
            
            self.selectResult = [NSString stringWithFormat:@"%@ %@",model.name,title];
            
        }else if (count==1){

            self.selectResult = [NSString stringWithFormat:@"%@ %@/斤",model.name,title];
        }else if (count==2){
            self.selectResult = [NSString stringWithFormat:@"%@ %@/天",model.name,title];
        }else if (count==3){
            self.selectResult = [NSString stringWithFormat:@"%@ %@/小时",model.name,title];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellHight + 0.1;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"%lu",(unsigned long)range.length);

    return YES;
}
@end
